package com.boc.langxiang.ui.activity.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.OtherSignInContract;
import com.boc.langxiang.entity.events.LoginSuccessEvent;
import com.boc.langxiang.entity.request.OtherSignInRequest;
import com.boc.langxiang.presenter.common.OtherSignInPresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OtherSignInActivity extends BaseToolBarActivity implements OtherSignInContract.View {

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.pwd_confirm)
    EditText pwdConfirm;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.activity_other_sign_in)
    LinearLayout activityOtherSignIn;

    private OtherSignInRequest mRequest;
    private OtherSignInPresenter mPresenter;

    private String mLx;
    private String mOpenid;

    @Override
    protected void onCreateCustomToolBar(Toolbar toolbar) {
        super.onCreateCustomToolBar(toolbar);
        setToolBarTitle("第三方账号注册");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_sign_in);
        ButterKnife.bind(this);
        mLx = getIntent().getStringExtra("lx");
        mOpenid = getIntent().getStringExtra("openid");
        mRequest = new OtherSignInRequest();
        mPresenter = new OtherSignInPresenter(this);
        initViews();
    }

    private void initViews() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNum = phone.getText().toString();
                String pwdStr = pwd.getText().toString();
                String pwdConfStr = pwdConfirm.getText().toString();
                mRequest.setPhone(phoneNum);
                mRequest.setPwd(pwdStr);
                mRequest.setPwdconf(pwdConfStr);
                mRequest.setOpenid(mOpenid);
                mRequest.setTypeid(mLx);
                if (StringUtil.isEmpty(phoneNum)) {
                    T.showShort(mContext, "请输入电话号码");
                    return;
                }
                if (StringUtil.isEmpty(pwdStr)) {
                    T.showShort(mContext, "请输入密码");
                    return;
                }
                if (StringUtil.isEmpty(pwdConfStr)) {
                    T.showShort(mContext, "请输入确认密码");
                    return;
                }

                mPresenter.confirm(mRequest);
            }
        });
    }

    @Override
    public void loginSuccess() {
        finish();
        ((Activity) Builder.mCtx).finish();
        EventBus.getDefault().post(new LoginSuccessEvent());
    }

    @Override
    public void showLoading() {
        showProgressDialog("请稍等");
    }

    @Override
    public void hideLoading() {
        super.hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }

    public static class Builder {
        private static Context mCtx;

        public Builder(Context ctx) {
            this.mCtx = ctx;
        }

        public void startActivty(String lx, String openid) {
            Intent intent = new Intent(mCtx, OtherSignInActivity.class);
            intent.putExtra("lx", lx);
            intent.putExtra("openid", openid);
            mCtx.startActivity(intent);
        }
    }
}
